package com.apotik.legenda.dialog;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JTextField;
import javax.swing.border.LineBorder;

import java.awt.Color;

import javax.swing.JButton;

import com.apotik.legenda.model.SalesTrxObat;
import com.apotik.legenda.observer.PosObserver;
import com.apotik.legenda.panel.PosPanel;
import com.apotik.legenda.util.AppUtil;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.math.BigDecimal;
import java.util.HashMap;

public class EditTrxObatDiscountDialog extends JDialog implements ActionListener, KeyListener {
	private static final long serialVersionUID = 1L;

	private JPanel panel;
	
	private JTextField textFieldName;
	private JTextField textFieldPrice;
	private JTextField textFieldDiscountPersen;
	private JTextField textFieldDiscountRp;
	private JTextField textFieldTotal;
	private JButton btnSubmit;
	private JButton btnCancel;
	
	private PosObserver observer;
	private int rowIndex;
	private SalesTrxObat salesTrxObat;
	
	public EditTrxObatDiscountDialog(PosObserver observer, int rowIndex, SalesTrxObat salesTrxObat) {
		this.observer = observer;
		this.rowIndex = rowIndex;
		this.salesTrxObat = salesTrxObat;
		
		setTitle("Edit Discount");
		setSize(379, 230);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		addKeyListener(this);
		
		panel = new JPanel();
		panel.setLayout(null);
		getContentPane().add(panel);
		
		JLabel lblName = new JLabel("Nama Barang");
		lblName.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblName.setBounds(15, 15, 90, 20);
		panel.add(lblName);
		
		textFieldName = new JTextField();
		textFieldName.setFont(new Font("Tahoma", Font.PLAIN, 12));
		textFieldName.setBounds(140, 13, 200, 22);
		textFieldName.setColumns(10);
		textFieldName.setEnabled(false);
		textFieldName.setText(salesTrxObat.getProductName());
		panel.add(textFieldName);
		
		JLabel lblPrice = new JLabel("Harga Satuan (Rp)");
		lblPrice.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblPrice.setBounds(15, 43, 119, 20);
		panel.add(lblPrice);
		
		textFieldPrice = new JTextField();
		textFieldPrice.setEnabled(false);
		textFieldPrice.setColumns(10);
		textFieldPrice.setFont(new Font("Tahoma", Font.PLAIN, 12));
		textFieldPrice.setBounds(140, 41, 200, 22);
		//textFieldPrice.setAlignmentX(RIGHT_ALIGNMENT);
		textFieldPrice.setText(AppUtil.formatStringIDR(salesTrxObat.getAmountUnitPrice().intValue()));
		panel.add(textFieldPrice);
		
		JLabel lblDiscountPersen = new JLabel("Discount (%)");
		lblDiscountPersen.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblDiscountPersen.setBounds(15, 71, 90, 20);
		panel.add(lblDiscountPersen);
		
		textFieldDiscountPersen = new JTextField();
		textFieldDiscountPersen.setColumns(10);
		textFieldDiscountPersen.setFont(new Font("Tahoma", Font.PLAIN, 12));
		textFieldDiscountPersen.setBounds(140, 69, 200, 22);
		textFieldDiscountPersen.setText(salesTrxObat.getDiscount().toString());
		textFieldDiscountPersen.addKeyListener(this);
		panel.add(textFieldDiscountPersen);
		
		JLabel lblDiscountRp = new JLabel("Discount (Rp)");
		lblDiscountRp.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblDiscountRp.setBounds(15, 99, 90, 20);
		panel.add(lblDiscountRp);
		
		textFieldDiscountRp = new JTextField();
		textFieldDiscountRp.setText("0");
		textFieldDiscountRp.setFont(new Font("Tahoma", Font.PLAIN, 12));
		textFieldDiscountRp.setEnabled(false);
		textFieldDiscountRp.setColumns(10);
		textFieldDiscountRp.setBounds(140, 97, 200, 22);
		panel.add(textFieldDiscountRp);
		
		JLabel lblTotal = new JLabel("Total");
		lblTotal.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblTotal.setBounds(15, 128, 90, 20);
		panel.add(lblTotal);
		
		textFieldTotal = new JTextField();
		textFieldTotal.setEnabled(false);
		textFieldTotal.setFont(new Font("Tahoma", Font.PLAIN, 12));
		textFieldTotal.setColumns(10);
		textFieldTotal.setBounds(140, 126, 200, 22);
		textFieldTotal.setText(AppUtil.formatStringIDR(salesTrxObat.getAmountTrxObat().intValue()));
		panel.add(textFieldTotal);
		
		btnSubmit = new JButton("Update");
		btnSubmit.setBounds(139, 155, 80, 25);
		btnSubmit.addActionListener(this);
		panel.add(btnSubmit);
		
		btnCancel = new JButton("Batal");
		btnCancel.setBounds(223, 155, 80, 25);
		btnCancel.addActionListener(this);
		panel.add(btnCancel);
		
		JPanel boxPanel = new JPanel();
		boxPanel.setBorder(new LineBorder(new Color(169, 169, 169)));
		boxPanel.setBounds(5, 4, 350, 183);
		panel.add(boxPanel);
	}
	
	
	
	public void actionPerformed(ActionEvent ae) {
		Object obj = ae.getSource();
		
		if(obj==btnCancel){
			this.setVisible(false);
		}else if(obj==btnSubmit){
			
			String discountStr = textFieldDiscountPersen.getText();
			if(discountStr.trim().isEmpty()){
				discountStr = "0";
			}
			
			int rowIndex = this.rowIndex;
			SalesTrxObat sto = this.salesTrxObat;
			int discount = Integer.parseInt(discountStr);
			
			BigDecimal sumHarga = AppUtil.amountSalesTrxObat(sto.getAmountUnitPrice(), sto.getQuantity(), sto.getAmountSC(), 0);
			BigDecimal discountAmount = AppUtil.amountPersentasi(discount, sumHarga, 0);
			BigDecimal sumHargaUpdate = sumHarga.subtract(discountAmount);
			
			HashMap<String, Object> hm = new HashMap<>();
			hm.put("update_for", PosPanel.UPDATE_DISCOUNT);
			hm.put("rowIndex", rowIndex);
			hm.put("salesTrxObat", sto);
			hm.put("discount", discount);
			hm.put("discountAmount", discountAmount);
			hm.put("sumHargaUpdate", sumHargaUpdate);
			
			observer.updateProduct(hm);
			
			this.setVisible(false);
		}
	}

	@Override
	public void keyPressed(KeyEvent ke) {
		if(ke.getKeyCode()==KeyEvent.VK_ESCAPE){
			this.setVisible(false);
		}
		
		//Object obj = ke.getSource();
	}

	@Override
	public void keyReleased(KeyEvent ke) {
		Object obj = ke.getSource();
		if(obj==textFieldDiscountPersen){
			if(ke.getKeyCode()==KeyEvent.VK_0 || ke.getKeyCode()==KeyEvent.VK_1 || ke.getKeyCode()==KeyEvent.VK_2 || 
					ke.getKeyCode()==KeyEvent.VK_3 || ke.getKeyCode()==KeyEvent.VK_4 || ke.getKeyCode()==KeyEvent.VK_5 || 
					ke.getKeyCode()==KeyEvent.VK_6 || ke.getKeyCode()==KeyEvent.VK_7 || ke.getKeyCode()==KeyEvent.VK_8 || 
					ke.getKeyCode()==KeyEvent.VK_9 || ke.getKeyCode()==KeyEvent.VK_BACK_SPACE || ke.getKeyCode()==KeyEvent.VK_DELETE){
				
				SalesTrxObat sto = this.salesTrxObat;
				int discount = Integer.parseInt(textFieldDiscountPersen.getText().trim());
				BigDecimal sumHarga = AppUtil.amountSalesTrxObat(sto.getAmountUnitPrice(), sto.getQuantity(), sto.getAmountSC(), 0);
				
				BigDecimal discountAmount = AppUtil.amountPersentasi(discount, sumHarga, 0);
				textFieldDiscountRp.setText(AppUtil.formatStringIDR(discountAmount.intValue()));
				
				BigDecimal sumHargaUpdate = sumHarga.subtract(discountAmount);
				textFieldTotal.setText(AppUtil.formatStringIDR(sumHargaUpdate.intValue()));
			}
		}
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
	}
}
